package ro.jproject.imc.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import ro.jproject.imc.model.DataBaseConnection;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class AddFacultyGUI extends JFrame {

	private JFrame frame;
	private JPanel mainPanel;
	private JButton save;
	private JButton buttonExit;
	private String saveFacultyName;
	private String saveAddress;
	private JTextField textFieldFacultyName;
	private JTextField textFieldAddress;
	private JLabel lblFacultyName;
	private JLabel lblAddress;

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({ })
	public AddFacultyGUI() {
		setTitle("universitys Registration");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(500, 200, 348, 202);

		frame = new JFrame();
		mainPanel = new JPanel();
		save = new JButton("Save");
		buttonExit = new JButton("Back");
		
		setContentPane(mainPanel);
		//frame.getContentPane().add(mainPanel);
		mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPanel.setLayout(null);

		save.setBounds(21, 121, 100, 32);
		mainPanel.add(save);


		buttonsAction();
		buttonExit.setBounds(206, 121, 100, 32);
		mainPanel.add(buttonExit);
		
		textFieldFacultyName = new JTextField();
		textFieldFacultyName.setBounds(108, 25, 198, 20);
		mainPanel.add(textFieldFacultyName);
		textFieldFacultyName.setColumns(10);
		
		textFieldAddress = new JTextField();
		textFieldAddress.setBounds(108, 64, 198, 20);
		mainPanel.add(textFieldAddress);
		textFieldAddress.setColumns(10);
		
		lblFacultyName = new JLabel("Faculty Name");
		lblFacultyName.setBounds(21, 28, 89, 14);
		mainPanel.add(lblFacultyName);
		
		lblAddress = new JLabel("Address");
		lblAddress.setBounds(23, 67, 75, 14);
		mainPanel.add(lblAddress);
		
		
	}

	void saveListener(ActionListener s) {
		save.addActionListener(s);
	}

	public void buttonsAction() {
		// save button
		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				saveFacultyName = textFieldFacultyName.getText();
				saveAddress = textFieldAddress.getText();
				

				

				DataBaseConnection connection = new DataBaseConnection();
				connection.connected();
				try {
					
						connection.insertQueryFaculty(saveFacultyName, saveAddress);
						
						JOptionPane.showMessageDialog(frame,"Registration saved!");
						
						textFieldFacultyName.setText("");
						textFieldAddress.setText("");
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		// exit button
		buttonExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				Ums frame = new Ums();
				frame.setVisible(true);
			}
		});
	}
}
